package com.zzhou.linkinfo.clock.mvp.releasetask;

import com.zzhou.linkinfo.clock.entity.Grade;
import com.zzhou.linkinfo.clock.mvp.IBasicView;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;

public class SelectContactsPresenter extends ISelectContactsContract.APresenter {
    ISelectContactsContract.IModel iModel;
    ISelectContactsContract.IView iView;

    public SelectContactsPresenter(IBasicView iView) {
        super(iView);
        iModel = new SelectContactsModel();
        this.iView = (ISelectContactsContract.IView) iView;
    }

    @Override
    public void request() {
        Observable observable = Observable.create(new ObservableOnSubscribe<List<Grade>>() {
            @Override
            public void subscribe(ObservableEmitter<List<Grade>> e) throws Exception {
                iModel.request(new HashMap<String, Object>());
            }
        });
        observable.compose(iView.bindLifecycle())
                .subscribe(new Consumer<List<Grade>>() {
                    @Override
                    public void accept(List<Grade> grades) throws Exception {
                        iView.onShowResult(true, grades);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
}
